package com.example.demo.service.impl;

import com.example.demo.mapper.GoodsMapper;
import com.example.demo.model.entity.Goods;
import com.example.demo.model.enums.ResultCodeEnum;
import com.example.demo.model.request.goods.CreateGoodsRequest;
import com.example.demo.model.request.goods.DeleteGoodsRequest;
import com.example.demo.model.request.goods.UpdateGoodsRequest;
import com.example.demo.model.response.PageQueryGoodsResponse;
import com.example.demo.service.GoodsService;
import com.example.demo.util.PageInfo;
import com.example.demo.util.PageUtil;
import com.example.demo.util.PromotionException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Boolean create(CreateGoodsRequest request) {
        Goods goods = buildGoods(request);
        goodsMapper.insert(goods);
        return true;
    }

    @Override
    public Boolean update(UpdateGoodsRequest request) {
        Goods existGoods = goodsMapper.selectById(request.getId());
        setGoods(existGoods, request);
        goodsMapper.update(existGoods);
        return true;
    }

    @Override
    public Boolean delete(DeleteGoodsRequest request) {
        goodsMapper.delete(request.getId());
        return true;
    }

    @Override
    public PageQueryGoodsResponse pageQuery(PageInfo pageInfo) {
        PageQueryGoodsResponse response = new PageQueryGoodsResponse();

        //分页查询商品
        Integer offset = pageInfo.getPageSize() * (pageInfo.getCurrent() - 1);
        List<Goods> goodsList = goodsMapper.pageQuery(pageInfo.getPageSize(), offset);
        response.setItems(goodsList);

        //查询总记录条数,并设置分页信息
        long totalItem = goodsMapper.getCount();
        setPageInfo(response, totalItem, pageInfo.getCurrent(), pageInfo.getPageSize());
        return response;
    }

    @Override
    public Goods query(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        if (null == goods) {
            throw PromotionException.fail(ResultCodeEnum.GOODS_NOT_EXISTED);
        }
        return goods;
    }

    /**
     * 设置response的分页信息
     *
     * @param response
     * @param totalItem
     * @param current
     * @param pageSize
     */
    private void setPageInfo(PageQueryGoodsResponse response, long totalItem, int current, int pageSize) {
        response.setTotal(totalItem);
        response.setCurrent(current);
        response.setPageSize(pageSize);
        response.setTotalPage(PageUtil.calculateTotalPage(totalItem, pageSize));
    }

    private Goods buildGoods(CreateGoodsRequest request) {
        Goods goods = new Goods();
        goods.setName(request.getName());
        goods.setPrice(request.getPrice());
        goods.setDescription(request.getDescription());
        goods.setImgUrl(request.getImg_url());
        return goods;
    }

    private Goods setGoods(Goods goods, UpdateGoodsRequest request) {
        if (null != request.getName()) {
            goods.setName(request.getName());
        }
        if (null != request.getPrice()) {
            goods.setPrice(request.getPrice());
        }
        if (StringUtils.isBlank(request.getDescription())) {
            goods.setDescription(request.getDescription());
        }
        if (StringUtils.isBlank(request.getImg_url())) {
            goods.setImgUrl(request.getImg_url());
        }
        return goods;
    }
}
